<?php
class Tournament extends Model {
	private $_TournamentId;
	private $_Class;
	private $_Surface;
	private $_Name;
	private $_Place;
	private $_Description;
	private $_InitialDate;
	private $_FinalDate;
	private $_Image;
	private $_Link;
	private $_Sex;
	private $_Points;
	private $_Active;
	private $_HasMatches;
	private $_Matches = array();
	
	public function getMatches(){
		return $this->_Matches;
	}

	public function setMatches($value){
		$this->_Matches = $value;
	}

	public function getHasMatches(){
		return $this->_HasMatches;
	}

	public function setHasMatches($value){
		$this->_HasMatches = $value;
	}
	
	public function getTournamentId(){
		return $this->_TournamentId;
	}
	
	public function getClass(){
		return $this->_Class;
	}
	
	public function getSurface(){
		return $this->_Surface;
	}
	
	public function getName(){
		return $this->_Name;
	}
	
	public function getPlace(){
		return $this->_Place;
	}
	
	public function getDescription(){
		return $this->_Description;
	}
	
	public function getInitialDate(){
		return $this->_InitialDate;
	}
	
	public function getFinalDate(){
		return $this->_FinalDate;
	}
	
	public function getImage(){
		return $this->_Image;
	}
	
	public function getLink(){
		return $this->_Link;
	}
	
	public function getSex(){
		return $this->_Sex;
	}
	
	public function getPoints(){
		return $this->_Points;
	}
	
	public function getActive(){
		return $this->_Active;
	}
	
	public function setTournamentId($value){
		$this->_TournamentId = $value;
	}
	
	public function setClass($value){
		$this->_Class = $value;
	}
	
	public function setSurface($value){
		$this->_Surface = $value;
	}
	
	public function setName($value){
		$this->_Name = $value;
	}

	public function setPlace($value){
		$this->_Place = $value;
	}

	public function setDescription($value){
		$this->_Description = $value;
	}

	public function setInitialDate($value){
		$this->_InitialDate = $value;
	}

	public function setFinalDate($value){
		$this->_FinalDate = $value;
	}

	public function setImage($value){
		$this->_Image = $value;
	}

	public function setLink($value){
		$this->_Link = $value;
	}

	public function setSex($value){
		$this->_Sex = $value;
	}
	
	public function setPoints($value){
		$this->_Points = $value;
	}

	public function setActive($value){
		$this->_Active = $value;
	}

	public function __construct($arrInitialValues = null){
		parent::__construct();

		if(isset($arrInitialValues['tournament_id']))
			$this->setTournamentId($arrInitialValues['tournament_id']);
		else
			$this->setTournamentId(0);

		if (isset($arrInitialValues['class']))
			$this->setClass($arrInitialValues['class']);
		else
			$this->setClass('');

		if (isset($arrInitialValues['surface']))
			$this->setSurface($arrInitialValues['surface']);
		else
			$this->setSurface('');

		if (isset($arrInitialValues['name']))
			$this->setName($arrInitialValues['name']);
		else
			$this->setName('');

		if (isset($arrInitialValues['place']))
			$this->setPlace($arrInitialValues['place']);
		else
			$this->setPlace('');

		if (isset($arrInitialValues['description']))
			$this->setDescription($arrInitialValues['description']);
		else
			$this->setDescription('');

		if (isset($arrInitialValues['initial_date']))
			$this->setInitialDate($arrInitialValues['initial_date']);
		else
			$this->setInitialDate(date('Y-m-d'));

		if (isset($arrInitialValues['final_date']))
			$this->setFinalDate($arrInitialValues['final_date']);
		else
			$this->setFinalDate(date('Y-m-d'));
			
		if (isset($arrInitialValues['image']))
			$this->setImage($arrInitialValues['image']);
		else
			$this->setImage('');

		if (isset($arrInitialValues['link']))
			$this->setLink($arrInitialValues['link']);
		else
			$this->setLink('');

		if (isset($arrInitialValues['sex']))
			$this->setSex($arrInitialValues['sex']);
		else
			$this->setSex('');

		if (isset($arrInitialValues['points']))
			$this->setPoints($arrInitialValues['points']);
		else
			$this->setPoints(0);
			
		if (isset($arrInitialValues['active']))
			$this->setActive($arrInitialValues['active']);
		else
			$this->setActive(0);
			
		if (isset($arrInitialValues['has_matches']))
			$this->setHasMatches($arrInitialValues['has_matches']);
		else
			$this->setHasMatches(0);
			
		if (isset($arrInitialValues['matches']))
			$this->setMatches($arrInitialValues['matches']);
		else
			$this->setMatches(array());
	}

	public static function getAll(){
		parent::requireDB();

		$query = '
			SELECT *
			FROM tournament';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}

	public function getTournament(){
		$query = '
			SELECT *
			FROM tournament
			WHERE tournament_id = ' . $this->getTournamentId();

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();		
	}

	public function getCurrentImage(){
		$query = '
			SELECT image
			FROM tournament
			WHERE tournament_id = ' . $this->getTournamentId();
		
		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();	
	}

	public function create(){
		$query = '
			INSERT INTO tournament
			(class_id, surface, name, place, description, initial_date, final_date, image, link, sex, points, active)
			VALUES 
			(' . $this->getClass()->getClassId() . ', "' . $this->getSurface() . '", "' . $this->getName() . '",
			"' . $this->getPlace() . '", "' . $this->getDescription() . '", 
			"' . date('Y-m-d', strtotime($this->getInitialDate())) . '", "' . date('Y-m-d', strtotime($this->getFinalDate())) . '", "' . $this->getImage() . '", "' . $this->getLink() . '", "' . $this->getSex() . '",
			' . $this->getPoints() . ', ' . $this->getActive() . ')';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getNumRows();
	}
	
	public function update(){
		$query = '
			UPDATE tournament
			SET class_id = ' . $this->getClass()->getClassId() .',
				surface = "' . utf8_decode($this->getSurface()) . '",
				name = "' . $this->getName() . '", 
				place = "' . utf8_decode($this->getPlace()) . '",
				description = "' . $this->getDescription() . '",
				initial_date = "' . $this->getInitialDate() . '",
				final_date = "' . $this->getFinalDate() . '",
				link = "' . $this->getLink() . '",
				sex = "' . $this->getSex() . '",
				points = ' . $this->getPoints() . ',
				active = ' . $this->getActive() . ' 
				WHERE tournament_id = ' . $this->getTournamentId();
				

		$dbHelper = new DBHelper($query);
		return 1;
	}
	
	public function alreadyExistsFemaleTournament(){
		$query = '
			SELECT 1 as exist
			FROM tournament
			WHERE name = "' . $this->getName() . '"
				AND sex = "F"';

		$dbHelper = new DBHelper($query);
		if ($dbHelper->getNumRows() == 0)
			return 0;

		$return = $dbHelper->getResult();
		return $return[0]['exist'];
		
	}

	public function updateImage(){
		$query='
			UPDATE tournament
			SET image = "' . $this->getImage() . '"
			WHERE tournament_id = ' . $this->getTournamentId();
		
		$dbHelper = new DBHelper($query);
		return ($dbHelper->getNumRows() > 0);
	}
	
	private function changeStatus(){
		$query='
			UPDATE tournament
			SET active = ' . $this->getActive() . '
			WHERE tournament_id = ' . $this->getTournamentId();

		$dbHelper = new DBHelper($query);
		return ($dbHelper->getNumRows() > 0);
	}
	
	public function activate(){
		$this->setActive(1);
		return $this->changeStatus();
	}
	
	public function delete(){
		$this->setActive(0);
		return $this->changeStatus();
	}
	
	public function getTournamentClass(){
		$query = '
			SELECT *
			FROM class
			WHERE class_id = ' . $this->getClass();
			
		$dbHelper = new DBHelper($query);
		$classDescription = $dbHelper->getResult();
		$this->setClass($classDescription[0]['description']);
	}
	
	private function getAmountOfMatches($table){
		$query = '
			SELECT *
			FROM ' . $table . '
			WHERE tournament_id = ' . $this->getTournamentId();

		$dbHelper = new DBHelper($query);
		$this->setHasMatches($dbHelper->getNumRows());
	}
	
	public function getIfHasMatches(){
		if (strtolower($this->getClass()) == 'nations')
			$table = 'serie'; 
		else
			$table = 'matches';
			
		$this->getAmountOfMatches($table);
	}
	
	public function getAllMatches(){
		$table = strtolower($this->getClass()) != 'nations' ? 'matches' : 'serie';
		return $this->_getMatches($table);
	}
	
	private function _getMatches($table){
		$query = '
			SELECT *
			FROM ' . $table . '
			WHERE tournament_id = ' . $this->getTournamentId() . '
			ORDER BY round_id ASC';
		if ($table == 'matches')
			$query .= ', match_id ASC';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	public function serieHasMatches($serie_id){
		$query = '
			SELECT COUNT(1) AS countMatches
			FROM international_matches
			WHERE serie_id = ' . $serie_id;

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
}
?>